<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>Document</title>
	<link rel="stylesheet" href="/assets/bootstrap/dist/css/bootstrap.min.css">
	<style type="text/css">
		.container {
			padding-top: 60px;
		}
		.padding {
			padding: 5px 0 20px 0;
		}
	</style>
</head>
<body>
	<div class="container">
		<div class="form-group">
			<label>请选择文件</label>
			<input type="file" id="file">
			<div class="padding" id="box">
				<!-- <img src="" alt="" class="img-rounded img-responsive"> -->
			</div>
			<div class="progress">
				<div class="progress-bar" style="width: 0%;" id="bar">0%</div>
			</div>
		</div>
	</div>
	<script>
		var file = document.getElementById('file');
		var bar = document.getElementById('bar');
		var box = document.getElementById('box');
		file.onchange = function(){
			var formData = new FormData();
			//将用户选择的文件追叫到formData表单对象中
			formData.append('attrName',this.files[0]);	

			var xhr = new XMLHttpRequest();
			xhr.open('post','http://localhost:3000/upload');
			//文件上传过程中持续触发
			xhr.upload.onprogress = function (ev) {
				//ev.loaded 文件上传了多少
				//ev.total 上传文件总大小
				var result = (ev.loaded / ev.total) * 100 + '%';
				bar.style.width = result;
				//将百分比显示子进度条中
				bar.innerHTML = result;
			  }
			xhr.send(formData);
			xhr.onload = function () {
				if(xhr.status == 200){	
					var result = JSON.parse(xhr.responseText);
					//动态创建img标签
					var img  = document.createElement('img');
					img.src = result.path;
					img.onload = function () {
						//图片加载完成后将图片显示在浏览器中
						box.appendChild(img);
					  }		
				}
			  }
		}
	</script>
</body>
</html>
